package com.zlf.sb.demo.ms;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/***
 *@title ThreadTest2
 *@description <TODO description class purpose>
 *@author wgc_j
 *@version 1.0.0
 *@create 2025-02-24 17:21
 **/
public class ThreadTest2 {
    public static void main(String[] args) {
// 获取可用的CPU核心数
        int corecount = Runtime.getRuntime().availableProcessors();
        System.out.println("Available CPu cores:" + corecount);
// 根据任务类型设定线程池大小
// 计算密集型任务推荐配置
        ExecutorService computeThreadPool = Executors.newFixedThreadPool(corecount);
// I/0密集型任务推荐配置(假设等待时间与计算时间接近)
        int maxIoTasks = corecount * 2;
// 实际值应根据测量的I0等待时间调整
        ExecutorService ioThreadPool = Executors.newFixedThreadPool(maxIoTasks);
// 提交一些示例任务给线程池
        for (int i = 0; i < 10; i++) {
            computeThreadPool.submit(() -> {
                System.out.println("Executing compute task on" + Thread.currentThread().getName());
            });
            ioThreadPool.submit(() -> {
                System.out.println("Executing Io task on " + Thread.currentThread().getName());
                try {
                    Thread.sleep(1000);// 模拟I0操作的等待时间lcatch(InterruptedExceptione){
                    Thread.currentThread().interrupt();
                } catch (Exception e) {
                    e.printStackTrace();
                    Thread.currentThread().interrupt();
                }
            });
        }
        computeThreadPool.shutdown();
        ioThreadPool.shutdown();
    }
}
